package v1

import (
	"bc/dao"
	"bc/service"
	"bc/util"
	"bc/util/library/zstack"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
)

type net_work struct {

}
/**
	创建普通二层网络
 */
func (t *net_work) Create(c *gin.Context){

	data := make(map[string]map[string]interface{})
	data["params"] = make(map[string]interface{})

	data["params"]["name"] = c.DefaultPostForm("name","")
	data["params"]["description"] = c.DefaultPostForm("description","")

	v,ok := c.GetPostForm("zoneUuid")
	if !ok{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请选择区域",
		})
		return
	}
	data["params"]["zoneUuid"] = v
	v,ok = c.GetPostForm("physicalInterface")
	if !ok {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"",
		})
		return
	}
	data["params"]["physicalInterface"] = v

	byte,err := json.Marshal(data)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}

	str,err := zstack.NetWork.CreateL2NoVlanNetwork(string(byte))
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":str,
	})

}
func (t *net_work)CreateCallBack(c *gin.Context){

	byte,err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		fmt.Println(err)
	}
	d := make(map[string]interface{})
	err = json.Unmarshal(byte,&d)

	nd := make(map[string]interface{})
	nd = d["inventory"].(map[string]interface{})
	nd["uid"] = c.GetInt("uid")
	sid := util.StrToInt(nil, c.Query("sid"))
	//场景id
	nd["sid"] = sid
	fmt.Println(d)
	if err != nil {
		fmt.Println(err)
	}
	b,i := service.L2Net.Create(nd)
	if b{
		fmt.Println(i)
	}

}
/**
	删除二层网络(DeleteL2Network)
 */
func (t *net_work) Delete (c *gin.Context){

	uuid := c.Param("uuid")
	if uuid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请选择需要删除的网络",
		})
		return
	}
	del_mode := "Permissive"

	str,err := zstack.NetWork.Delete(uuid,del_mode)
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请重试",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"error",
		"data":str,
	})

}
func (t *net_work) DeleteCallBack (c *gin.Context){

	b,_ := ioutil.ReadAll(c.Request.Body)

	m := make(map[string]interface{})
	err := json.Unmarshal(b,&m)

	if err != nil{
		panic(err)
	}
	if v,ok := m["error"];ok{
		errstr := v.(map[string]interface{})["description"]
		fmt.Println(errstr)
		return
	}

	uuid := c.Query("uuid")
	if uuid == ""{
		panic("错误")
	}
	bool := service.L2Net.Delete(map[string]interface{}{"uuid":uuid})
	if bool {

	}

}
/**
	创建三层网络
 */
func (t *net_work)CreateL3(c *gin.Context){

	data := make(map[string]map[string]interface{})
	data["params"] = make(map[string]interface{})

	if v,ok := c.GetPostForm("name");ok{
		data["params"]["name"] = v
	}
	data["params"]["type"] = c.DefaultPostForm("type","L3BasicNetwork")
	data["params"]["description"] = c.DefaultPostForm("description","")
	l2NetworkUuid,ok := c.GetPostForm("l2NetworkUuid")
	if !ok{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请选择二层网络",
		})
		return
	}

	if dnsDomain , ok := c.GetPostForm("dnsDomain");ok{
		data["params"]["dnsDomain"] = dnsDomain
	}

	data["params"]["l2NetworkUuid"] = l2NetworkUuid
	if v,ok := c.GetPostForm("system");ok{
		data["params"]["system"] = v
		category := "System"
		if v == "false"{
			category = "Private"
		}
		data["params"]["category"] = category
	}
	byte,err := json.Marshal(data)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"请重试",
		})
		return
	}
	str,err := zstack.NetWork.CreateL3Network(string(byte))
	if err != nil {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":str,
	})

}
func (t *net_work)L3CreatCallBack(c *gin.Context){

	b,er := ioutil.ReadAll(c.Request.Body)
	if er != nil{
		panic(er)
	}
	defer c.Request.Body.Close()

	m := make(map[string]interface{})
	err := json.Unmarshal(b,&m)

	if err != nil{
		panic(err)
	}
	if v,ok := m["error"];ok{
		fmt.Println(v.(map[string]interface{})["details"])
		return
	}
	if v,ok := m["inventory"];ok{

		inventory := v.(map[string]interface{})
		inventory["uid"] = c.GetInt("uid")
		fmt.Println(inventory)
		bool := service.L3Net.Create(inventory)
		if bool{
			c.JSON(200,gin.H{
				"code":200,
				"msg":"操作成功",
			})
			return
		}
	}

}
func (t *net_work)L3NetworkDelete(c *gin.Context){

	uuid := c.Param("uuid")
	if uuid == ""{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"uuid不能为空",
		})
		return
	}
	del_mode := "Permissive"
	str,err := zstack.NetWork.DeleteL3Network(uuid,del_mode)
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err,
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"error",
		"data":str,
	})

}
func (t *net_work)L3NetWorkDeleteCallBack(c *gin.Context){

	uuid := c.Query("uuid")
	if uuid == ""{
		return
	}
	byte,err := ioutil.ReadAll(c.Request.Body)
	if err != nil{
		panic(err)
	}
	res := make(map[string]interface{})
	err = json.Unmarshal(byte,&res)
	if err != nil{
		panic(err)
	}
	if v,ok := res["error"];ok{
		fmt.Println(v.(map[string]interface{})["details"])
		return
	}
	bool := service.L3Net.Delete(map[string]interface{}{"uuid":uuid})
	if !bool{
		return
	}
}

/**
创建VXLAN网络
*/
//func (t *net_work) CreateL2VxlanNetwork(c *gin.Context){
//
//	data := make(map[string]map[string]interface{})
//	data["params"] = make(map[string]interface{})
//
//	data["params"]["name"] = c.DefaultPostForm("name","")
//	data["params"]["description"] = c.DefaultPostForm("description","")
//
//	v,ok := c.GetPostForm("zoneUuid")
//	if !ok{
//		c.JSON(200,gin.H{
//			"code":500,
//			"msg":"请选择区域",
//		})
//		return
//	}
//	data["params"]["zoneUuid"] = v
//	v,ok = c.GetPostForm("physicalInterface")
//	if !ok {
//		c.JSON(200,gin.H{
//			"code":500,
//			"msg":"error",
//			"data":"",
//		})
//		return
//	}
//	data["params"]["physicalInterface"] = v
//
//	byte,err := json.Marshal(data)
//
//	if err != nil{
//		c.JSON(200,gin.H{
//			"code":500,
//			"msg":"error",
//			"data":err.Error(),
//		})
//		return
//	}
//
//	str,err := zstack.NetWork.CreateL2VxlanNetwork(string(byte))
//	if err != nil{
//		c.JSON(200,gin.H{
//			"code":500,
//			"msg":"error",
//			"data":err.Error(),
//		})
//		return
//	}
//	c.JSON(200,gin.H{
//		"code":200,
//		"msg":"操作成功",
//		"data":str,
//	})
//
//}

/**
创建VXLAN网络
*/
func (t *net_work) CreateL2VlanNetwork(c *gin.Context){

	data := make(map[string]map[string]interface{})
	data["params"] = make(map[string]interface{})

	data["params"]["name"] = c.DefaultPostForm("name","")
	data["params"]["vlan"] = c.DefaultPostForm("vlan","")
	data["params"]["description"] = c.DefaultPostForm("description","")
	fmt.Println(data)
	v,ok := c.GetPostForm("zoneUuid")
	if !ok{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"请选择区域",
		})
		return
	}
	data["params"]["zoneUuid"] = v
	v,ok = c.GetPostForm("physicalInterface")
	if !ok {
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":"",
		})
		return
	}
	data["params"]["physicalInterface"] = v

	byte,err := json.Marshal(data)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}

	str,err := zstack.NetWork.CreateL2VlanNetwork(string(byte))
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":str,
	})

}
/**
	修改2层网络
 */
func (t *net_work)  Update(c *gin.Context)  {
	uuid := c.Param("uuid")
	data := make(map[string]map[string]interface{})
	data["updateL2Network"] = make(map[string]interface{},1)

	data["updateL2Network"]["name"] = c.DefaultPostForm("name","")
	data["updateL2Network"]["description"] = c.DefaultPostForm("description","")

	byte,err := json.Marshal(data)

	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}

	str,err := zstack.NetWork.UpdateL2Network(uuid, string(byte))
	if err != nil{
		c.JSON(200,gin.H{
			"code":500,
			"msg":"error",
			"data":err.Error(),
		})
		return
	}
	c.JSON(200,gin.H{
		"code":200,
		"msg":"操作成功",
		"data":str,
	})
}

func (t *net_work)UpdateCallBack(c *gin.Context)  {
	uuid := c.Query("uuid")
	if uuid == ""{
		return
	}
	byte,err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	res := make(map[string]interface{},1)
	json.Unmarshal(byte, &res)
	data := res["inventory"].(map[string]interface{})

	dao.L2Net.UpdateByUuid(uuid, data)
}

var NetWork = &net_work{}